package com.mycompany.biblio.business;

import com.mycompany.biblio.model.Comment;
import com.mycompany.biblio.model.Status;
import com.mycompany.biblio.model.User;
import java.util.Date;
import java.util.List;
import java.util.ListIterator;
import javax.ejb.EJB;
import javax.persistence.*;
import javax.ejb.Stateless;


@Stateless
public class StatusDAO {

    // ======================================
    // =             Attributes             =
    // ======================================
    
    
    @EJB
    UserDAO userDAO;
    
    @EJB
    FriendDAO friendDAO;
    
    @EJB
    SecurityService securityService;
    
    @PersistenceContext(unitName = "jsfExamplePU")
    private EntityManager em;

    // ======================================
    // =           Public Methods           =
    // ======================================

    public List<Status> getAllStatuses()
    {
        User currentUser = securityService.getCurrentUser();
        //receuperer tous les status qui sont publiques, et ceux des amis
        Query query = em.createNamedQuery(Status.GET_ALL);  
        List<Status> liStatus = query.getResultList();
        ListIterator<Status> iterator = liStatus.listIterator();
        while (iterator.hasNext())
        {
            Status s = iterator.next();
            //remove those are other's private, those not of current user's friend
            switch (s.getVision())
            {
                case Status.PUBLIC:
                    //donothing
                    break;
                case Status.FRIEND_ONLY:
                    //neither mine nor myfriend's
                    if (!s.getProprietaire().getUsername().equals(currentUser.getUsername()) && !friendDAO.isFriend(s.getProprietaire(),currentUser))
                        iterator.remove();
                    break;
                case Status.PRIVATE:
                    //not mine
                    if (!s.getProprietaire().getUsername().equals(currentUser.getUsername()))
                        iterator.remove();
                    break;
            }
        }
        return liStatus;        
    }
    
    public List<Status> getOwnStatuses()
    {
        User user = securityService.getCurrentUser();
        return user.getStatuses();
    }

    public Status create(Status Status) {
        em.persist(Status);
        return Status;
    }

    public Status update(Status status) {
        return em.merge(status);
    }

    public void likeStatus(User user, Status status)
    {
        status.getListLike().add(user);
        user.getLikedStatus().add(status);
        userDAO.update(user);
        update(status);
    }
    
    public boolean hasLike(User user, Status status)
    {
        for(User u : status.getListLike())
        {
            if ((u!=null) && user.getUsername().equals(u.getUsername()))
                return true;
        }
        return false;
    }

    public Status getByID(int statusID)
    {
        Query query = em.createNamedQuery(Status.GET_BY_NAME).setParameter(1, statusID);       
        return (Status)query.getResultList().get(0);
    }

    public void unlikeStatus(User currentUser, Status status)
    {
        //get iterator and remove
        ListIterator<User> liUser= status.getListLike().listIterator();
        while (liUser.hasNext())
        {
            User u = liUser.next();
            if (u.getUsername().equals(currentUser.getUsername()))
            {
                liUser.remove();
                break;
            }
        }
        
        ListIterator<Status> liStatus= currentUser.getLikedStatus().listIterator();
        while (liStatus.hasNext())
        {
            Status s = liStatus.next();
            if (s.getIDStatut()==status.getIDStatut())
            {
                liStatus.remove();
                break;
            }
        }              
        userDAO.update(currentUser);
        update(status);
    }

    public void addComment(Status status, String newComment) 
    {
        Comment comment = new Comment();
        comment.setCommenter(securityService.getCurrentUser());
        comment.setDatePublication(new Date());
        comment.setContenu(newComment);
        comment.setStatus(status);        
        status.getListComments().add(comment);
        status.getNewComment().setValue("");
        update(status);        
    }
}
